package com.yjhealth.libs.businessdemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yjhealth.libs.businessdemo.R;
import com.yjhealth.libs.commonlib.base.fragment.BaseFragment;

import java.util.Date;

public class DemoFragment extends BaseFragment {

    private static final String ARG_DATA = "DATA";
    
    TextView tvContent;
    Handler handler = new Handler();
    
    String argData;

    public static DemoFragment newInstance(String data) {
        DemoFragment fragment = new DemoFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_DATA, data);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            argData = getArguments().getString(ARG_DATA);
        }
    }
    
    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.businessdemo_fragment_demo, container,false);
    }

    @Override
    protected void viewCreated(View view, @Nullable Bundle savedInstanceState) {
        initLayout();
        tvContent = view.findViewById(R.id.tvContent);
    }

    @Override
    protected void onRefreshView() {
        taskGetData();
    }

    @Override
    protected void startHint() {
        if (isLazyLoaded()) {
            return;
        }
        taskGetData();
        setLazyLoaded(true);
    }
    
    private void taskGetData(){
        showLoadingDialog();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissLoadingDialog();
                Date date = new Date();
                tvContent.setText(argData + date.toLocaleString());
            }
        }, 1500);
    }
}
